package com.dap.system.office.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.dap.common.core.utils.ServletUtils;
import com.dap.common.security.service.TokenService;
import com.dap.system.api.model.LoginUser;
import com.dap.system.office.domain.TClass;
import com.dap.system.office.domain.vo.TStudentScoreInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dap.common.log.annotation.Log;
import com.dap.common.log.enums.BusinessType;
import com.dap.common.security.annotation.PreAuthorize;
import com.dap.system.office.domain.TScore;
import com.dap.system.office.service.ITScoreService;
import com.dap.common.core.web.controller.BaseController;
import com.dap.common.core.web.domain.AjaxResult;
import com.dap.common.core.utils.poi.ExcelUtil;
import com.dap.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 分数Controller
 *
 * @author dap
 * @date 2021-11-12
 */
@RestController
@RequestMapping("/office/score")
public class TScoreController extends BaseController
{
    @Autowired
    private ITScoreService tScoreService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询人列表
     */
    @PreAuthorize(hasPermi = "office:score:list")
    @GetMapping("/list")
    public TableDataInfo list(TScore tScore)
    {
        List<TScore> list = tScoreService.selectTScoreListPage(tScore);
        return getDataTable(list);
    }

    /**
     * 查询每个学生的分数
     */
    @PreAuthorize(hasPermi = "office:score:list")
    @GetMapping("/studentScoreInfo")
    public TableDataInfo studentScoreInfo(TScore tScore)
    {
        List<TStudentScoreInfoVo> list = tScoreService.selectStudentScoreInfo(tScore);
        return getDataTable(list);
    }

    /**
     * 导出分数列表
     */
    @PreAuthorize(hasPermi = "office:score:export")
    @Log(title = "分数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TScore tScore) throws IOException
    {
        List<TScore> list = tScoreService.selectTScoreList(tScore);
        ExcelUtil<TScore> util = new ExcelUtil<TScore>(TScore.class);
        util.exportExcel(response, list, "score");
    }
    /**
     * 导入分数列表
     */
    @PreAuthorize(hasPermi = "office:score:import")
    @Log(title = "分数列表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TScore> util = new ExcelUtil<TScore>(TScore.class);
        List<TScore> scoreList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = tScoreService.importScore(scoreList, updateSupport, operName);
        return AjaxResult.success(message);

    }
    // 下载模板方法
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<TClass> util = new ExcelUtil<TClass>(TClass.class);
        util.importTemplateExcel(response, "班级列表");
    }



    /**
     * 获取分数详细信息
     */
    @PreAuthorize(hasPermi = "office:score:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tScoreService.selectTScoreById(id));
    }

    /**
     * 新增分数
     */
    @PreAuthorize(hasPermi = "office:score:add")
    @Log(title = "分数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TScore tScore)
    {
        return toAjax(tScoreService.insertTScore(tScore));
    }

    /**
     * 修改分数
     */
    @PreAuthorize(hasPermi = "office:score:edit")
    @Log(title = "分数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TScore tScore)
    {
        return toAjax(tScoreService.updateTScore(tScore));
    }

    /**
     * 删除分数
     */
    @PreAuthorize(hasPermi = "office:score:remove")
    @Log(title = "分数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tScoreService.deleteTScoreByIds(ids));
    }
}
